package com.example.internetwork;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private Button button1,button2,button3;
    private ImageView imageView;
    private OkHttpClient okHttpClient1,okHttpClient2,okHttpClient3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=findViewById(R.id.button1);
        button2=findViewById(R.id.button2);
        button3=findViewById(R.id.button3);
        imageView=findViewById(R.id.imageView);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test1();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test2();

            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test3();

            }
        });
    }
    public void test1(){
       okHttpClient1 = new OkHttpClient();
        Request request1 = new Request.Builder().get().
                url("https://www.httpbin.org/image/png").build();
        Call call1 = okHttpClient1.newCall(request1);
        call1.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream is = response.body().byteStream();
                final Bitmap bitmap = BitmapFactory.decodeStream(is);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageBitmap(bitmap);
                    }
                });
                is.close();
            }
        });
    }
    public void test2(){
        okHttpClient2 = new OkHttpClient();
        Request request2 = new Request.Builder().get().
                url("https://www.httpbin.org/get").
               /* addHeader("key","value").*/
                build();
        Call call2 = okHttpClient2.newCall(request2);
        call2.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("okhttp",response.body().string());
            }
        });
    }
    public void test3(){
        okHttpClient3 = new OkHttpClient();
        Request request3 = new Request.Builder().get().
                url("https://www.httpbin.org/ip").
                /* addHeader("key","value").*/
                        build();
        Call call3 = okHttpClient3.newCall(request3);
        call3.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("okhttp",response.body().string());
            }
        });
    }

}